package User;

import com.opensymphony.xwork2.ActionSupport;
import db_tool.DBConnection;
import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserQuery extends ActionSupport {
    public String username = null;
    public Statement statement = null;
    public DBConnection user = null;
    public ResultSet resultSet = null;

    public String getData() throws Exception { //按照用户姓名查询其子节点数据
        try {
            this.user = DBConnection.getInstance();//建立连接
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String sql = "SELECT username FROM [Account] where superiorName='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        System.out.println(sql);
        try {
            this.resultSet = this.statement.executeQuery(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String user_name = null;
        String data = "user";
        try {
            while (this.resultSet.next()) {
                user_name = resultSet.getString(1);
            /*String password=resultSet.getString(2)+":";
			String TelePhone=resultSet.getString(3)+":";
			String Email=resultSet.getString(4)+":";
			String Description=resultSet.getString(5)+":";
			String active=resultSet.getString(6)+":";
			String superior=resultSet.getString(7)+":";
			String roleName=resultSet.getString(8)+";"*/
                data += ":";
                data += user_name;
			/*data+=password;
			data+=TelePhone;
			data+=Email;
			data+=Description;
			data+=active;
			data+=superior;
			data+=roleName;*/

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            writer.write("error");
            return null;
        }
        if (data == "user") {
            writer.write("");
            return null;
        }
        data = data.replace("user:", "");
        System.out.println(data);

        writer.write(data);
        return null;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String userQuery() throws Exception//按照用户姓名查询其子节点数据
    {
        try {
            this.user = DBConnection.getInstance();//建立连接
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        String sql = "SELECT * FROM [Account] where username='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        String sql = "SELECT * FROM [Account] where username='" + this.username + "'";

        System.out.println(sql);
        try {
            this.resultSet = this.statement.executeQuery(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String data = "";
        try {
            while (this.resultSet.next()) {
                String user_name = resultSet.getString(2) + ":";
                String password = resultSet.getString(3) + ":";
                String TelePhone = resultSet.getString(4) + ":";
                String Email = resultSet.getString(5) + ":";
                String Description = resultSet.getString(6) + ":";
                String superior = resultSet.getString(8) + ":";
                String roleName = resultSet.getString(9);
                data += user_name;
                data += password;
                data += TelePhone;
                data += Email;
                data += Description;
                data += superior;
                data += roleName;

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            writer.write("error");
            return null;
        }
        System.out.println(data);
        data = data.replace("user;", "");

        writer.write(data);
        return null;
    }

    public String deleteQuery() throws Exception {

        try {
            this.user = DBConnection.getInstance();//建立连接
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        try {
            this.statement = this.user.getStatement();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int result = 0;
        //String sql = "delete FROM [Account] where username='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        String sql = "delete FROM [Account] where username='" + this.username + "'";

        System.out.println(sql);
        try {
            result = this.statement.executeUpdate(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (result >= 1) {
            writer.write("success");
        } else {
            writer.write("failure");
        }
        return null;
    }

    public String getidentify() throws IOException, SQLException {
        try {
            this.user = DBConnection.getInstance();//建立连接
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ResultSet result = null;
        //        String sql = "select roleName FROM [Account] where username='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        String sql = "select roleName FROM [Account] where username='" + this.username + "'";
        System.out.println(sql);
        try {
            result = this.statement.executeQuery(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String identify;
        if (result.next()) {
            identify = result.getString("roleName");
            System.out.println(identify);
            writer.write(identify);
        }
        return null;
    }

    public String idQuery() throws IOException, SQLException {
        try {
            this.user = DBConnection.getInstance();//建立连接
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ResultSet result = null;
        String sql = "select id FROM [Account] where username='" + this.username + "'";

//        String sql = "select id FROM [Account] where username='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        System.out.println(sql);
        try {
            result = this.statement.executeQuery(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String identify;
        if (result.next()) {
            identify = result.getString("id");
            System.out.println(identify);
            writer.write(identify);
        }
        return null;
    }

    public String  activeQuery() throws IOException, SQLException {

        try {
            this.user = DBConnection.getInstance();//建立连接
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        try {
            this.statement = this.user.getStatement();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ResultSet result = null;
        String sql = "select active FROM [Account] where username='" + this.username + "'";
//        String sql = "select active FROM [Account] where usernme='" + new String(this.username.getBytes("iso8859-1"), "utf-8") + "'";
        System.out.println(sql);
        try {
            result = this.statement.executeQuery(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String identify;
        if (result.next()) {
            identify = result.getString("active");
            System.out.println(identify);
            writer.write(identify);
        }
        return null;
    }

}
